extends Node2D

#cardView列表
var list=[]
const centerX=640 #中间位置 用来控制牌的摆放
const normalY=600

var checkCard=null setget setCheckCard #S_idle时鼠标指着的卡牌


var state_idle=IdleState.new()
var state_follow=FollowState.new()
var state_confirm=ConfirmState.new()
var state_disable=DisableState.new()
var state=state_idle setget setState               #保持状态的引用

func setState(value):
	state._exit()
	state=value
	state._enter()
	pass

func setCheckCard(value):
	#print("checkCard")
	if checkCard!=null:
		checkCard.large=0
		Map.getM2V(checkCard.card.user).unSelect()
	checkCard=value
	
	
	if checkCard!=null:
		checkCard.large=1
		updateCheckCardPosition()
		#chessView选中指示
		Map.getM2V(checkCard.card.user).select()
	else:
		updateCardPosition()
	pass


func _ready():
	state_idle.start(self)
	state_follow.start(self)
	state_confirm.start(self)
	state_disable.start(self)
	# Called when the node is added to the scene for the first time.
	# Initialization here
	pass

func append(card):
	var handCard=load("res://View/HandCard.tscn").instance()
	add_child(handCard)
	handCard.loadCard(card)
	handCard.position=Vector2(1280,normalY)
	list.append(handCard)
	
	updateCardPosition()
#普通跟新卡牌位置
func updateCardPosition():
	var centerI=floor(list.size()-1)/2 #位于中间的I值
	for i in range(list.size()):
		var handCard=list[i]
		handCard.targetPosition=Vector2((i-centerI)*100+centerX,normalY)
		handCard.z_index=i
#鼠标浏览卡牌时跟新卡牌位置
func updateCheckCardPosition():
	var j=list.find(checkCard) #当前checkCard所处的位置
	var centerI=floor(list.size()-1)/2 #位于中间的I值
	for i in range(list.size()):
		var handCard=list[i]
		if i<j:
			handCard.targetPosition=Vector2((i-centerI)*100+centerX-0.1*i*100,normalY)
			handCard.z_index=i
		elif i>j:
			handCard.targetPosition=Vector2((i-centerI)*100+centerX+0.1*i*100,normalY)
			handCard.z_index=i
		elif i==j:
			handCard.targetPosition=Vector2((i-centerI)*100+centerX,normalY-50)
			handCard.position=Vector2((i-centerI)*100+centerX,normalY-50)
			handCard.z_index=20
	pass


func _physics_process(delta):
	state._physics_process(delta)

func useHandCard(handCard,target=null):
	var result=Global.CardSystem.useCard(handCard.card,target)
	if result==0:
		#使用失败 取消
		checkCard.state=1
		setState(state_idle)
		return
#使用成功后丢弃或消耗卡牌
	if result==1:

		self.checkCard=null
		setState(state_idle)
#		discard(handCard)
#		setState(state_idle)
#		#触发卡牌的效果
#		var card=handCard.card
#		var donghua=card.useFunc.call_func(card.user,target)
#		Global.Battle.appendDonghua(donghua)
		
		pass
		
	pass
#丢弃卡牌，包括使用，弃牌，除外，只处理view-handCard，逻辑上不管
func discard(card):
	var handCard=cardToHandCard(card)
	if handCard!=null:
		list.erase(handCard)
		handCard.discard()  #手牌丢弃S_disCard
	pass
func discardAll():
	for handCard in list:
		discard(handCard)
#通过card找到HandCard
func cardToHandCard(card):
	var handCard=null
	for h in list:
		if h.card==card:
			handCard=h
	return handCard

#--------------------------------------------------------

class State extends Reference:
	
	var target
	func start(target_):
		target=target_
	func _physics_process(delta):
		pass
	func _enter():
		pass
	func _exit():
		pass

#--------------------------------
class IdleState extends State:
	func _physics_process(delta):
		var results=target.get_world_2d().direct_space_state.intersect_point(target.get_viewport().get_mouse_position(),32,[],2)
		var card=getMaxZIndexCard(results)
		if target.checkCard!=card:
			target.setCheckCard(card)
		if Input.is_action_just_pressed("mouseLeft"):
			if target.checkCard!=null:
				target.setState(target.state_follow)
		pass
	func _enter():
		target.updateCardPosition()
		pass
	func _exit():
		pass
	func getMaxZIndexCard(results):
		if results.size()==0:
			return null
		var handCard=null
		for result in results:
			var card=result.collider.get_node("..")
			if handCard==null:
				handCard=card
			else:
				if card.z_index>handCard.z_index:
					handCard=card
		return handCard

#=====================================
class FollowState extends State:
	func _physics_process(delta):
		if Input.is_action_just_pressed("mouseRight"):
				target.checkCard.state=1
				target.checkCard.position=target.checkCard.targetPosition
				target.setState(target.state_idle)
		#确认
		if target.get_viewport().get_mouse_position().y<450:
			#判断能量够不够
			var energy=Global.CardSystem.energy
			if energy<target.checkCard.card.cost:
				#取消
				target.checkCard.state=1
				target.checkCard.position=target.checkCard.targetPosition
				target.setState(target.state_idle)
			else:
				#确认
				target.setState(target.state_confirm)
	func _enter():
		target.checkCard.state=2
		pass
	func _exit():
		pass
		
#-----------------------------------------
class ConfirmState extends State:
	#鼠标当前指着的棋子
	var pointTarget=null setget set_pointTarget
	var useType=0 #卡牌的使用类型 0敌人指向 1友军指向 2全部指向 3不指向
	
	func set_pointTarget(value):
		if pointTarget!=null:
			var view=Map.getM2V(pointTarget)
			if view!=null:
				view.dePoint()
		
		pointTarget=value
		if pointTarget!=null:
			var view=Map.getM2V(pointTarget)
			if view!=null:
				view.point()
		pass
	func _physics_process(delta):
		#aoe的情况
		if useType==3:
			#卡牌跟着鼠标运动
			var handCard=target.checkCard
			handCard.global_position=target.get_global_mouse_position()
			#取消
			if Input.is_action_just_pressed("mouseRight") || target.get_global_mouse_position().y>500:
				target.checkCard.state=1
				target.setState(target.state_idle)
			#使用卡牌
			elif Input.is_action_just_released("mouseLeft"):
					target.useHandCard(target.checkCard,null)
					pass
		else:
			#指向型技能的情况
			#跟新箭头位置
			Global.arrow.reset(target.checkCard.global_position,target.get_global_mouse_position())
			var mask=0
			match useType:
				0:
					mask=8
				1:
					mask=4
				2:
					mask=12

			#获得鼠标当前指着的棋子
			var results=target.get_world_2d().direct_space_state.intersect_point(target.get_global_mouse_position(),32,[],mask)
			var t=null
			if results.size()>0:
				t=results[0].collider.get_node("..").chess
			if t!=pointTarget:
					self.pointTarget=t
			#取消
			if Input.is_action_just_pressed("mouseRight") || target.get_global_mouse_position().y>500:
				target.checkCard.state=1
				target.setState(target.state_idle)
			#使用卡牌
			elif Input.is_action_just_released("mouseLeft") &&pointTarget!=null:
					target.useHandCard(target.checkCard,pointTarget)
					pass
	func _enter():
		var card=target.checkCard
		card.state=3
		useType=card.card.useType
		if useType<=2:
			card.targetPosition=Vector2(640,500)
		#显示箭头
		Global.arrow.reset(target.checkCard.global_position,target.get_global_mouse_position())
		if useType<=2:
			Global.arrow.visible=true
		
	func _exit():
		self.pointTarget=null
		#隐藏箭头
		Global.arrow.visible=false
#------------------------------------------------
#回合结束，回合未开始，不能操作 
class DisableState extends State: 
	